<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'wms:pu:rd01:wmsRd01:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno })">
          {{ record.djno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsPuRd01WmsRd01List">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { wmsRd01Delete, wmsRd01ListData } from '/@/api/wms/pu/rd01/wmsRd01';
  import { basWarehousetreeData } from '/@/api/bas/house/basWarehouse';
  
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './viewForm.vue';

  const { t } = useI18n('wms/pu.rd01.wmsRd01');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('采购入库单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('入库单号'),
        field: 'djno',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'basInv.invCode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.invName',
        component: 'Input',
      },
      {
        label: t('规格型号'),
        field: 'basInv.invStd',
        component: 'Input',
      },
      {
        label: t('采购单号'),
        field: 'poCode',
        component: 'Input',
      },
      {
        label: t('入库日期'),
        field: 'ddate',
        component: 'RangePicker',
      },
      {
        label: t('仓库'),
        field: 'whCode',
        component: 'TreeSelect',
        componentProps: {
          api: basWarehousetreeData,
          params: { isLoadUser: true, userIdPrefix: '',isAll:true },
          canSelectParent: false,
          allowClear: true,
          // treeCheckable: true,
        },
      },
      {
        label: t('制单人'),
        field: 'updateByName',
        component: 'Input',
      },
    ],
    fieldMapToTime: [['ddate', ['ddate_gte', 'ddate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('入库单号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('入库日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.invCode',
      key: 'inv.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.invName',
      key: 'inv.inv_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.invStd',
      key: 'inv.inv_std',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.unitName',
      key: 'inv.unit_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('采购单号'),
      dataIndex: 'poCode',
      key: 'a.po_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('供应商编码'),
      dataIndex: 'pomain.venCode',
      key: 'pomain.ven_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('供应商名称'),
      dataIndex: 'pomain.venName',
      key: 'pomain.ven_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库编码'),
      dataIndex: 'whCode',
      key: 'a.wh_code',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('仓库名称'),
      dataIndex: 'basWare.cwhname',
      key: 'a.wh_code',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('合同号'),
      dataIndex: 'podetails.extend.cdefine28',
      sorter: false,
      width: 130,
      align: 'left',
    },
    {
      title: t('付款方式'),
      dataIndex: 'podetails.extend.cdefine29',
      sorter: false,
      width: 130,
      align: 'left',
    },
    {
      title: t('计重方式'),
      dataIndex: 'podetails.extend.cdefine30',
      sorter: false,
      width: 130,
      align: 'left',
    },
    {
      title: t('税率'),
      dataIndex: 'itaxrate',
      key: 'a.itaxrate',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('汇率'),
      dataIndex: 'iexchrate',
      key: 'a.iexchrate',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('创建日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑采购入库单'),
        onClick: handleForm.bind(this, { djno: record.djno }),
        auth: 'wms:pu:rd01:wmsRd01:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除采购入库单'),
        popConfirm: {
          title: t('是否确认删除采购入库单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wms:pu:rd01:wmsRd01:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: wmsRd01ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { djno: record.djno };
    const res = await wmsRd01Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
